<?php

/**
 * 短信服务类
 */

namespace App\Common\Sms;

use App\Common\ErrorCode;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class Sms
{
    /**
     * 发送短信
     * @param $smsConfig Model 短信配置
     * @param $mobile
     * @param $tpl_id
     * @param $data
     * @return array|bool
     */
    public static function send($smsConfig, $mobile, $tpl_id, $data)
    {
        $smstype = $smsConfig['smstype'];
        $params = sms_data($smstype, $data);
        if ($smstype == 1) {
            $params = "mobile=" . $mobile . "&tpl_id=" . $tpl_id . "&tpl_value=" . $params . "&key=" . $smsConfig['appkey'];
            $url = 'http://v.juhe.cn/sms/send';
            $result = ihttp_post($url, $params);
            if (empty($result) || (0 < $result['error_code'])) {
                return error(1, '短信发送失败(' . $result['error_code'] . ')：' . $result['reason']);
            }
            return true;
        }
        if ($smstype == 2) {
            include_once ROOT_PATH . '/app/Common/Sms/dayu/TopSdk.php';

            $dayuClient = new \TopClient();
            $dayuClient->appkey = $smsConfig['app_key'];
            $dayuClient->secretKey = $smsConfig['app_serve'];
            $dayuRequest = new \AlibabaAliqinFcSmsNumSendRequest();
            $dayuRequest->setSmsType('normal');
            $dayuRequest->setSmsFreeSignName($smsConfig['signname']);
            $dayuRequest->setSmsParam($params);
            $dayuRequest->setRecNum('' . $mobile);
            $dayuRequest->setSmsTemplateCode($tpl_id);
            $dayuResult = $dayuClient->execute($dayuRequest);
            $dayuResult = (array)$dayuResult;
            if (empty($dayuResult) || !empty($dayuResult['code'])) {
                return error(1, '短信发送失败(' . $dayuResult['sub_msg'] . '/code: ' . $dayuResult['code'] . '/sub_code: ' . $dayuResult['sub_code'] . ')');
            }
            return true;
        }
        if ($smstype == 3) {
            require_once ROOT_PATH . '/app/Common/Sms/alisms/SmsApi.php';
            date_default_timezone_set("Asia/Shanghai");
            set_time_limit(0);
            $alisms = new \SmsApi($smsConfig['access_key_id'], $smsConfig['access_key_secret']);
            $response = $alisms->sendSms(
                $smsConfig['aliaccsignname'],
                $tpl_id,
                $mobile,
                $data
            );

            $res = json_decode(json_encode($response), 1);
            if ($res['Code'] != 'OK') {
                return error(1, '短信发送失败(错误信息: ' . $res['Message'] . ')');
            }
            return true;
        }
        return error(1, '没有设定的短信类型');
    }

    /**
     * 检测用户短信验证码
     * @param $code
     * @param $type
     * @param $mobile
     * @return bool
     */
    public static function checkAuthCode($code, $type, $mobile)
    {
        $code = trim($code);//短信验证码
        $redisKey = "sms_auth_data_{$type}_{$mobile}";
        $smsData = Redis::get($redisKey);

        if (!$smsData) {
            return error(ErrorCode::SMSCODE_ERROR, '验证码错误!');
        }

        //验证码错误
        if ($code != $smsData) {
            return error(ErrorCode::SMSCODE_ERROR, '验证码错误!');
        }

        return true;
    }
}